package com.erp.common.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Eric
 * @Date: 2019/6/29 17:45
 * @Description:判断两个相同对象不同的数据
 */
public class GetDifference {

    public static Map<String, String> getObject(Object a, Object b)
            throws IllegalArgumentException, IllegalAccessException {
        Map<String, String> map = new HashMap<String, String>();
        Field[] declaredFields2 = a.getClass().getDeclaredFields();
        Field[] declaredFields23 = b.getClass().getDeclaredFields();
        StringBuilder xiugaiqian = new StringBuilder();
        StringBuffer xiugaihou = new StringBuffer();
        for (int i = 0; i < declaredFields2.length; i++) {
            declaredFields2[i].setAccessible(true);
            declaredFields23[i].setAccessible(true);
            if (declaredFields2[i].get(a) != null && declaredFields23[i].get(b) != null) {
                if ("userIdCreate".equals(declaredFields2[i].getName()) || "userNameCreate".equals(declaredFields2[i].getName())
                        || "gmtCreate".equals(declaredFields2[i].getName()) || "gmtModified".equals(declaredFields2[i].getName())
                        || "userIdModified".equals(declaredFields2[i].getName()) || "userNameModified".equals(declaredFields2[i].getName())){
                    System.out.println("固定字段不吉利");
                }else{
                    if (!declaredFields2[i].get(a).equals(declaredFields23[i].get(b))) {
                        xiugaiqian.append(declaredFields2[i].getName() + ":" + declaredFields2[i].get(a)).append(",");
                        xiugaihou.append(declaredFields23[i].getName() + ":" + declaredFields23[i].get(b)).append(",");
                    }
                }

            } else if (declaredFields2[i].get(a) == null && declaredFields23[i].get(b) != null) {
                if ("userIdCreate".equals(declaredFields2[i].getName()) || "userNameCreate".equals(declaredFields2[i].getName())
                        || "gmtCreate".equals(declaredFields2[i].getName()) || "gmtModified".equals(declaredFields2[i].getName())
                        || "userIdModified".equals(declaredFields2[i].getName()) || "userNameModified".equals(declaredFields2[i].getName())){
                    System.out.println("固定字段不吉利");
                }else{
                    xiugaiqian.append(declaredFields2[i].getName() + ":" + null).append(",");
                    xiugaihou.append(declaredFields23[i].getName() + ":" + declaredFields23[i].get(b)).append(",");
                }

            } else if (declaredFields2[i].get(a) != null && declaredFields23[i].get(b) == null) {
                if ("userIdCreate".equals(declaredFields2[i].getName()) || "userNameCreate".equals(declaredFields2[i].getName())
                        || "gmtCreate".equals(declaredFields2[i].getName()) || "gmtModified".equals(declaredFields2[i].getName())
                        || "userIdModified".equals(declaredFields2[i].getName()) || "userNameModified".equals(declaredFields2[i].getName())){
                    System.out.println("固定字段不吉利");
                }else{
                    xiugaiqian.append(declaredFields2[i].getName() + ":" + declaredFields2[i].get(a)).append(",");
                    xiugaihou.append(declaredFields23[i].getName() + ":" + null).append(",");
                }
            }
        }
        if (StringUtils.isNoneBlank(xiugaiqian.toString()) && StringUtils.isNoneBlank(xiugaihou.toString())) {
            map.put(xiugaiqian.toString().substring(0, xiugaiqian.length() - 1),
                    xiugaihou.toString().substring(0, xiugaihou.length() - 1));
        }
        return map;

    }

}
